package com.ruoyi.teach.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.teach.utils.MultiRequestBody;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teach.domain.NbQuestion;
import com.ruoyi.teach.service.INbQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题目Controller
 * 
 * @author ruoyi
 * @date 2021-12-08
 */
@RestController
@RequestMapping("/teach/question")
public class NbQuestionController extends BaseController
{
    @Autowired
    private INbQuestionService nbQuestionService;

    /**
     * 查询题目列表
     */
    @PreAuthorize("@ss.hasPermi('teach:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(NbQuestion nbQuestion)
    {
        startPage();
        List<NbQuestion> list = nbQuestionService.selectNbQuestionList(nbQuestion);
        return getDataTable(list);
    }

    /**
     * 导出题目列表
     */
    @PreAuthorize("@ss.hasPermi('teach:question:export')")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NbQuestion nbQuestion)
    {
        List<NbQuestion> list = nbQuestionService.selectNbQuestionList(nbQuestion);
        ExcelUtil<NbQuestion> util = new ExcelUtil<NbQuestion>(NbQuestion.class);
        util.exportExcel(response, list, "题目数据");
    }

    /**
     * 获取题目详细信息
     */
    @PreAuthorize("@ss.hasPermi('teach:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(nbQuestionService.selectNbQuestionById(id));
    }

    /**
     * 新增题目
     */
    @PreAuthorize("@ss.hasPermi('teach:question:add')")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NbQuestion nbQuestion)
    {
        return toAjax(nbQuestionService.insertNbQuestion(nbQuestion));
    }

    /**
     * 修改题目
     */
    @PreAuthorize("@ss.hasPermi('teach:question:edit')")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NbQuestion nbQuestion)
    {
        return toAjax(nbQuestionService.updateNbQuestion(nbQuestion));
    }

    /**
     * 删除题目
     */
    @PreAuthorize("@ss.hasPermi('teach:question:remove')")
    @Log(title = "题目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(nbQuestionService.deleteNbQuestionByIds(ids));
    }


    /**
     * 批量添加题目
     */
    @Log(title = "教师出题", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batch(@MultiRequestBody("questions") List<JSONObject> questions){
        Long id = getUserId();
        nbQuestionService.batchQuestions(questions, id);
        return AjaxResult.success();
    }

}
